import { getCategoryBreadAPI } from '@/api/index.js'
import { onMounted, ref } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'

export function useCategory() {
  const route = useRoute()
  const categoryData = ref([])
  const getBread = async (id = route.params.id) => {
    const res = await getCategoryBreadAPI(id)
    categoryData.value = res.result
  }
  onMounted(() => getBread())
  // 导航变化后
  onBeforeRouteUpdate((to) => {
    getBread(to.params.id)
  })
  return {
    categoryData
  }
}